import datetime

def dateutil(begin, end):
    d = begin
    delta = datetime.timedelta(days=1)
    sql = ''
    while d <= end:
        # print(d.strftime("%Y-%m-%d 00:00:00"))
        sql += '(SELECT COUNT(DISTINCT user_id) FROM audio_user_listen_log WHERE listen_time BETWEEN "%s" AND "%s") %s, \n' % (d.strftime("%Y-%m-%d 00:00:00"), d.strftime("%Y-%m-%d 23:59:59"), 'date' + d.strftime("%Y%m%d"))
        # print(sql)
        d += delta
    return sql[:-3]

print('1======================================================================')
sqlstr = dateutil(datetime.date(2018,1,1), datetime.date(2018,1,31))
print(sqlstr)
print('2======================================================================')
sqlstr = dateutil(datetime.date(2018,2,1), datetime.date(2018,2,28))
print(sqlstr)
print('3======================================================================')
sqlstr = dateutil(datetime.date(2018,3,1), datetime.date(2018,3,31))
print(sqlstr)
print('4======================================================================')
sqlstr = dateutil(datetime.date(2018,4,1), datetime.date(2018,4,30))
print(sqlstr)
print('5======================================================================')
sqlstr = dateutil(datetime.date(2018,5,1), datetime.date(2018,5,31))
print(sqlstr)
print('6======================================================================')
sqlstr = dateutil(datetime.date(2018,6,1), datetime.date(2018,6,30))
print(sqlstr)
print('7======================================================================')
sqlstr = dateutil(datetime.date(2018,7,1), datetime.date(2018,7,31))
print(sqlstr)
print('8======================================================================')
sqlstr = dateutil(datetime.date(2018,8,1), datetime.date(2018,8,31))
print(sqlstr)
print('9======================================================================')
sqlstr = dateutil(datetime.date(2018,9,1), datetime.date(2018,9,30))
print(sqlstr)
print('10======================================================================')
sqlstr = dateutil(datetime.date(2018,10,1), datetime.date(2018,10,31))
print(sqlstr)
print('11======================================================================')
sqlstr = dateutil(datetime.date(2018,11,1), datetime.date(2018,11,30))
print(sqlstr)
print('12======================================================================')
sqlstr = dateutil(datetime.date(2018,12,1), datetime.date(2018,12,31))
print(sqlstr)








